package com.hhh.springai_test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.model.dto.aiPrompt.AiPromptAddRequest;
import com.hhh.springai_test.model.dto.aiPrompt.AiPromptUpdateRequest;
import com.hhh.springai_test.model.po.AiPrompt;
import com.hhh.springai_test.service.AiPromptService;
import com.hhh.springai_test.mapper.AiPromptMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author 29915
* @description 针对表【ai_prompt(AI Prompt表)】的数据库操作Service实现
* @createDate 2025-02-05 20:21:29
*/
@Service
public class AiPromptServiceImpl extends ServiceImpl<AiPromptMapper, AiPrompt>
    implements AiPromptService{

    @Override
    public long addAiPrompt(AiPromptAddRequest aiPromptAddRequest) {
        if (aiPromptAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        if (StringUtils.isBlank(aiPromptAddRequest.getPrompttitle())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题不能为空");
        }
        if (StringUtils.isBlank(aiPromptAddRequest.getPromptcontent())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "内容不能为空");
        }
        
        // 判断标题是否已存在
        QueryWrapper<AiPrompt> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prompttitle", aiPromptAddRequest.getPrompttitle());
        long count = this.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "标题已存在");
        }
        AiPrompt aiPrompt = new AiPrompt();
        BeanUtils.copyProperties(aiPromptAddRequest, aiPrompt);
        boolean result = this.save(aiPrompt);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "添加失败");
        }
        return aiPrompt.getId();
    }

    @Override
    public boolean deleteAiPrompt(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不合法");
        }
        AiPrompt aiPrompt = this.getById(id);
        if (aiPrompt == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "数据不存在");
        }
        return this.removeById(id);
    }

    @Override
    public boolean updateAiPrompt(AiPromptUpdateRequest aiPromptUpdateRequest) {
        if (aiPromptUpdateRequest == null || aiPromptUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        
        AiPrompt aiPrompt = this.getById(aiPromptUpdateRequest.getId());
        if (aiPrompt == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "数据不存在");
        }
        
        BeanUtils.copyProperties(aiPromptUpdateRequest, aiPrompt);
        return this.updateById(aiPrompt);
    }

    @Override
    public List<AiPrompt> listAiPrompt() {
        QueryWrapper<AiPrompt> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createtime");
        return this.list(queryWrapper);
    }
}




